/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.ds2.cafeteria.vista;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import mx.itschapala.ds2.cafeteria.bl.UsuarioBlLocal;
import mx.itschapala.ds2.cafeteria.modelo.Usuario;

/**
 *
 * @author Santiago
 */
@ManagedBean
@SessionScoped
public class LoginBean {

    @EJB
    private UsuarioBlLocal usuarioBl;
    private String user;
    private String pass;
    public String noControl;
    FacesContext context;
    public LoginBean() {
        
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public String validarLogin() {
        String page = "";
        for (Usuario usu : usuarioBl.getAll()) {
            if (user.equals(String.valueOf(usu.getNoControl())) && pass.equals(String.valueOf(usu.getContrasena())) && !user.equals("Administrador") && !pass.equals("1")) {
                context = FacesContext.getCurrentInstance();
                context.getExternalContext().getSessionMap().clear();
                context.getExternalContext().getSessionMap().put("USUARIO_LOGEADO", user);
                page = "MenuPrincipalUsuario";
            } else if (user.equals("Administrador") && pass.equals("1")) {
                context = FacesContext.getCurrentInstance();
                context.getExternalContext().getSessionMap().clear();
                context.getExternalContext().getSessionMap().put("USUARIO_LOGEADO", user);
                page = "MenuPrincipal";
            }
        }
        return page;
    }
}
